home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / RO101.ZIP / RO_POST.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  6KB  |  307 lines

  1. /********************************************************/
  2. /*                            */
  3. /*    ro_post.c    post processor for ro        */
  4. /*                            */
  5. /*    ro version 1.10                    */
  6. /*                            */
  7. /*    Copyright (c) 1990 by Ted A. Campbell        */
  8. /*        Bywater Software            */
  9. /*        P. O. Box 4023                */
  10. /*        Duke Station                */
  11. /*        Durham, NC  27706            */
  12. /*                            */
  13. /* Permission is hereby granted for all commercial and    */
  14. /* non-commercial reproduction and distribution of this */
  15. /* material provided this notice is included.        */
  16. /*                            */
  17. /********************************************************/
  18.  
  19. #include "ro.h"
  20.  
  21. /* #define    DEBUG_PP */
  22.  
  23. #ifdef    POSTPROCESS
  24.  
  25. static    char    ps_tbuf[ PP_STRINGSIZE ];
  26. static     char     temp[ PP_STRINGSIZE ];
  27. static    int    pp = 0;
  28.  
  29. /***    Storage Areas for Printer/Terminal Control Strings ***/
  30.  
  31. char    ps_init[ PP_STRINGSIZE ] = { 0 };
  32. char    ps_deinit[ PP_STRINGSIZE ] = { 0 };
  33. char    ps_bdon[ PP_STRINGSIZE ] = { 0 };
  34. char    ps_bdoff[ PP_STRINGSIZE ] = { 0 };
  35. char    ps_iton[ PP_STRINGSIZE ] = { 0 };
  36. char    ps_itoff[ PP_STRINGSIZE ] = { 0 };
  37. char    ps_hup[ PP_STRINGSIZE ] = { 0 };
  38. char    ps_hdn[ PP_STRINGSIZE ] = { 0 };
  39.  
  40. /****************************************************************
  41.  
  42.     pp_init()    Initialize post-processor
  43.  
  44. ****************************************************************/
  45.  
  46. pp_init( name )
  47.    char *name;
  48.    {
  49.    FILE *fp;
  50.  
  51.    /*** Attempt to open the post-processor TABfile ***/
  52.  
  53.    if ( ( fp = fopen( name, "r" )) == NULL )
  54.       {
  55.       fprintf( stderr, "Cannot open TABfile %s. \n", name );
  56.       pp = 0;
  57.       return -1;
  58.       }
  59.  
  60.    /*** Read the name of the printer/terminal device ***/
  61.  
  62.    if ( feof( fp ) == 0 )
  63.       {
  64.       fgets( ps_tbuf, PP_STRINGSIZE - 1, fp );
  65.       }
  66.  
  67.    if ( ro_verbose == TRUE )
  68.       {
  69.       fprintf( stderr, "Preparing output for %s \n", ps_tbuf );
  70.       }
  71.  
  72.    /*** Read the remainder of the post-processor TABfile ***/
  73.  
  74.    while ( feof( fp ) == 0 )
  75.       {
  76.       fgets( ps_tbuf, PP_STRINGSIZE - 1, fp );
  77.       pp_read( ps_tbuf );
  78.       }
  79.  
  80.    /*** Close the post-processor TABfile ***/
  81.  
  82.    fclose( fp );
  83.    pp = 1;
  84.  
  85.    /*** Send out initialization string ***/
  86.  
  87.    pp_puts( ps_init );
  88.  
  89.    }
  90.  
  91. pp_read( s )
  92.    char *s;
  93.    {
  94.    register int c;
  95.  
  96.    /*** First read to whitespace to find identifier ***/ 
  97.  
  98.    c = 0;
  99.    temp[ c ] = 0;
  100.    while ( isspace( s[ c ] ) == 0 )
  101.       {
  102.       temp[ c ] = s[ c ];
  103.       temp[ ++c ] = 0;
  104.       }
  105.  
  106. #ifdef   DEBUG_PP
  107.    fprintf( stderr, "DEBUG pp_init(): read string <%s> \n", s );
  108.    fprintf( stderr, "DEBUG pp_init(): found identifier <%s> \n", temp );
  109. #endif
  110.  
  111.    /*** See if the identifier matches our descriptions ***/
  112.  
  113.    if ( strcmp( temp, "twinit" ) == 0 )
  114.       {
  115.       pp_getstr( s, ps_init );
  116.       }
  117.    else if ( strcmp( temp, "twrest" ) == 0 )
  118.       {
  119.       pp_getstr( s, ps_deinit );
  120.       }
  121.    else if ( strcmp( temp, "bdon" ) == 0 )
  122.       {
  123.       pp_getstr( s, ps_bdon );
  124.       }
  125.    else if ( strcmp( temp, "bdoff" ) == 0 )
  126.       {
  127.       pp_getstr( s, ps_bdoff );
  128.       }
  129.    else if ( strcmp( temp, "iton" ) == 0 )
  130.       {
  131.       pp_getstr( s, ps_iton );
  132.       }
  133.    else if ( strcmp( temp, "itoff" ) == 0 )
  134.       {
  135.       pp_getstr( s, ps_itoff );
  136.       }
  137.    else if ( strcmp( temp, "up" ) == 0 )
  138.       {
  139.       pp_getstr( s, ps_hup );
  140.       }
  141.    else if ( strcmp( temp, "down" ) == 0 )
  142.       {
  143.       pp_getstr( s, ps_hdn );
  144.       }
  145.    else 
  146.       {
  147.       return -1;
  148.       }
  149.    }
  150.  
  151. pp_getstr( s, buf )
  152.    char *s, *buf;
  153.    {
  154.    register int c, d;
  155.    char delimiter;
  156.  
  157. #ifdef   DEBUG_PP
  158.       fprintf( stderr, "\tDEBUG pp_getstr():  called \n" );
  159. #endif
  160.  
  161.    /*** First wind past identifier ***/ 
  162.  
  163.    c = 0;
  164.    while ( isspace( s[ c ] ) == 0 )
  165.       {
  166.       ++c;
  167.       }
  168.  
  169.    /*** Now wind past whitespace ***/
  170.  
  171.    while( isspace( s[ c ] ) != 0 )
  172.       {
  173.       ++c;
  174.       }
  175.  
  176.    /*** Identify delimiter ***/
  177.  
  178.    delimiter = s[ c ];
  179.    ++c;
  180.  
  181. #ifdef   DEBUG_PP
  182.       fprintf( stderr, "\tDEBUG pp_getstr():  delimiter is 0x%x <%c> \n", 
  183.          delimiter, delimiter );
  184. #endif
  185.  
  186.    /*** Process individual characters of the string ***/
  187.  
  188.    d = 0;
  189.    while ( ( s[ c ] != delimiter ) && ( s[ c ] != 0 ))
  190.       {
  191.  
  192. #ifdef   DEBUG_PP
  193.       fprintf( stderr, "\tDEBUG pp_getstr():  char 0x%x <%c> \n", s[ c ], s[ c ] );
  194. #endif
  195.  
  196.       switch( s[ c ] )
  197.          {
  198.          case '\\':
  199.             ++c;
  200.             switch( s[ c ] )
  201.                {
  202.                case '0':
  203.                   buf[ d ] = ( s[ c + 1 ] - '0' ) * 010 
  204.                      + ( s[ c + 2 ] - '0' );
  205.                   c += 2;
  206.                   ++d;
  207.                   buf[ d ] = 0;
  208.                default:
  209.                   break;
  210.                }
  211.             break;
  212.          default:
  213.             buf[ d ] = s[ c ];
  214.             ++d;
  215.             buf[ d ] = 0;
  216.             break;
  217.          }
  218.       ++c;
  219.       }
  220.    }
  221.  
  222. /****************************************************************
  223.  
  224.    ro_post()   Post-processor output function
  225.  
  226. ****************************************************************/
  227.  
  228. ro_post( c )
  229.    int c;
  230.    {
  231.    static int esc_pending = 0;
  232.  
  233.    if ( pp == 0 )
  234.       {
  235.       putchar ( c );
  236.       return;
  237.       }
  238.  
  239.    if ( esc_pending == 0 )
  240.       {
  241.       switch( c )
  242.          {
  243.          case ESCAPE:
  244.             esc_pending = 1;
  245.             return;
  246.          default:
  247.             putchar( c );
  248.             return;
  249.          }
  250.       }
  251.    else
  252.       {
  253.       esc_pending = 0;
  254.       switch( c )
  255.          {
  256.          case ROMAN:
  257.             pp_puts( ps_bdoff );
  258.             pp_puts( ps_itoff );
  259.             break;
  260.          case ITALIC:
  261.             pp_puts( ps_iton );
  262.             break;
  263.          case BOLD:
  264.             pp_puts( ps_bdon );
  265.             break;
  266.          case HALFUP:
  267.             pp_puts( ps_hup );
  268.             break;
  269.          case HALFDOWN:
  270.             pp_puts( ps_hdn );
  271.             break;
  272.          default:
  273.             fprintf( stderr, "Unrecognized output escape sequence ESC-0x%x \n", c );
  274.             break;
  275.          }
  276.       }
  277.    }
  278.  
  279. /****************************************************************
  280.  
  281.    pp_deinit()   Deinitialize post-processor
  282.  
  283. ****************************************************************/
  284.  
  285. pp_deinit()
  286.    {
  287.    if ( pp == 1 )
  288.       {
  289.       pp_puts( ps_deinit );
  290.       }
  291.    }
  292.  
  293. pp_puts( s )
  294.    char *s;
  295.    {
  296.    register int c;
  297.  
  298.    for ( c = 0; s[ c ] != 0; ++c )
  299.       {
  300.       putchar( s[ c ] );
  301.       }
  302.  
  303.    }
  304.  
  305. #endif
  306.  
  307.